package com.juyu.miliao.authentication.service;

import com.juyu.miliao.authentication.dao.mysql.AppkeyDao;
import com.juyu.miliao.authentication.domain.Appkey;
import com.juyu.miliao.authentication.service.cache.AppKeyCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class AppKeyService {
    @Resource
    private AppKeyCacheService appKeyCacheService;
    @Resource
    private AppkeyDao appkeyDao;

    /**
     * 获取参数
     * @param key
     * @return
     */
    public Appkey getAppKeyByKey(String key){
        List<Appkey> appKeyList = appKeyCacheService.getAppKeyList();
        Appkey returnAppKey = new Appkey();
        for (Appkey appkey: appKeyList ) {
            if(appkey.getKey().equals(key)){
                returnAppKey = appkey;
                break;
            }
        }
        return returnAppKey;
    }

    public int updateIsH5Buy(String val){
        return appkeyDao.updateIsH5Buy(val);
    }

    /**
     * 合并根据key获取配置内容
     * @param key
     * @return
     */
    public List<Appkey> getAppKeyByKeyList(String key) {
        List<Appkey> appKeyList = appKeyCacheService.getAppKeyList();
        List<Appkey> showList = new ArrayList<>();
        if(key.contains(",")){
            String [] array = key.split(",");
            for(int i = 0,len =array.length;i<len;i++){
                for (Appkey appkey: appKeyList) {
                    if(appkey.getKey().equals(array[i])){
                        showList.add(appkey);
                    }
                }
            }
        }else {
            for (Appkey appkey: appKeyList ) {
                if(appkey.getKey().equals(key)){
                    showList.add(appkey);
                }
            }
        }
     return showList;
    }
}
